package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Reply;
import dbconn.DBConnect;

/*****
 * 回复留言类
 * 回复留言
 * 删除回复
 * 获取单个留言所有回复
 * @author Administrator
 *
 */
public class ReplyControl {
	public static boolean Reply(Reply r) {
		DBConnect dbConnect = new DBConnect();
		String sql = "insert into reply(targetid,content,addtime) values(?,?,?)";
		dbConnect.prepareStatement(sql);
		try {
			dbConnect.setInt(1, r.getTargetid());
			dbConnect.setString(2, r.getContent());
			dbConnect.setString(3, r.getAddtime());
			dbConnect.executeUpdate();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			dbConnect.close();
		}
	}
	
	public static boolean deleteReply(int id){
		DBConnect dbc = new DBConnect();
		String sql = "delete from reply where id=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	public static List<Reply> getAllReplies(int targetId){
		DBConnect dbConnect = new DBConnect();
		List<Reply> list = new ArrayList<Reply>();
		String sql = "select * from reply where targetid= '"+targetId+"'";
		ResultSet rSet = null; 
		dbConnect.prepareStatement(sql);
		try {
			rSet = dbConnect.executeQuery();
			while(rSet.next()){
				Reply reply = new Reply();
				reply.setId(rSet.getInt("id"));
				reply.setContent(rSet.getString("content"));
				reply.setTargetid(rSet.getInt("targetid"));
				reply.setAddtime(rSet.getString("addtime"));
				list.add(reply);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbConnect.close();
		}
	}
	

}
